package com.codi.api.controller.wap;

import com.codi.api.security.UserUtil;
import com.codi.base.domain.BaseResult;
import com.codi.base.log.annotation.Loggable;
import com.codi.bus.core.service.FundNetValueBackService;
import com.codi.bus.core.service.TradeOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/web/net")
public class WapFundNetValueController {
    @Autowired
    private FundNetValueBackService fundNetValueBackService;


    @Autowired
    private TradeOrderService tradeOrderService;

    @GetMapping
    public BaseResult getByFundCode(String fundCode) throws Exception{
        return BaseResult.success(fundNetValueBackService.getOneYearNetValueByFundCode(fundCode));
    }

    @GetMapping("/one")
    public BaseResult getOneMonth(String fundCode) throws Exception {
        return BaseResult.success(fundNetValueBackService.getOneMonthNetValueByFundCode(fundCode));
    }

    @GetMapping("/three")
    public BaseResult getThreeMonth(String fundCode) throws Exception {
        return BaseResult.success(fundNetValueBackService.getThreeMonthNetValueByFundCode(fundCode));
    }

    @GetMapping("/six")
    public BaseResult getSixMonth(String fundCode) throws Exception {
        return BaseResult.success(fundNetValueBackService.getSixMonthNetValueByFundCode(fundCode));
    }

    @PostMapping("/trading")
    public BaseResult getTrading(String fundCode) throws Exception {
        Long userId = UserUtil.getLoginUserId();

        if (userId == null) {
            return BaseResult.success();
        }

        return BaseResult.success(fundNetValueBackService.getTradeDay(fundCode, userId));
    }

    @Loggable
    @PostMapping("/sellTrading")
    public BaseResult getSellTrading(String fundCode) throws Exception {
        Long userId = UserUtil.getLoginUserId();

        if (userId == null) {
            return BaseResult.success();
        }

        return BaseResult.success(tradeOrderService.getSellConfirmDates(fundCode, userId));
    }

    @Loggable
    @GetMapping("/get_history_net_values")
    public BaseResult getH(String fundCode, Pageable pageable) throws Exception {
        return BaseResult.success(fundNetValueBackService.queryPages(pageable, fundCode));
    }

    @Loggable
    @GetMapping("/get_history_mm")
    public BaseResult getHM(String fundCode, Pageable pageable) throws Exception {
        return BaseResult.success(fundNetValueBackService.queryMMPages(pageable, fundCode));
    }
}
